package com.example.sls.controller.admin.defaultTable;

import com.example.sls.pojo.admin.defaultTable.EmpDt;
import com.example.sls.utils.DateUtil;
import com.example.sls.service.admin.defaultTable.EmpDtService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/empdt")
public class EmpDtController {
    @Autowired
    EmpDtService empDtService;

    @PostMapping("/addData")
    public Map<String,String> addData(@RequestParam Map<String, String> params) throws ParseException {

        Map<String,String> result = new HashMap<>();
//        System.out.println("========");
//        System.out.println(params);
//        System.out.println("========");
        Integer id = empDtService.getMaxId() + 1;
        String workno = params.get("workno");
        String name = params.get("name");
        Integer gender = Integer.valueOf(params.get("gender"));
        Integer age = Integer.valueOf(params.get("age"));
        String idcard = params.get("idcard");
        String address = params.get("address");
        Date entrydate = DateUtil.String2Date(params.get("entrydate"));

        EmpDt emp = new EmpDt(id, workno, name, gender, age, idcard, address, entrydate);


        if(empDtService.addData(emp)){
            result.put("error_message","success");
        }
        else{
            result.put("error_message","failed");
        }
        return result;
    }

    @PostMapping("/updateData")
    public Map<String,String> updateData(@RequestParam Map<String, String> params) throws ParseException {

        Map<String,Object>emp = new HashMap<>();
//        System.out.println("========");
//        System.out.println(params);
//        System.out.println("========");
        emp.put("id", Integer.parseInt(params.get("id")));
        emp.put("workno", params.get("workno"));
        emp.put("name", params.get("name"));
        emp.put("gender", Integer.parseInt(params.get("gender")));
        emp.put("age", Integer.valueOf(params.get("age")));
        emp.put("idcard", params.get("idcard"));
        emp.put("address", params.get("address"));
        emp.put("entrysate",DateUtil.String2Date(params.get("entrydate")));

        Map<String,String> result = new HashMap<>();
        if(empDtService.updateData(emp)){
            result.put("error_message","success");
        }
        else{
            result.put("error_message","failed");
        }
        return result;

    }

    @PostMapping("/deleteData")
    public Map<String,String> deleteData(@RequestParam Map<String, String> params){

        Integer id = Integer.parseInt(params.get("id"));
        Map<String,String> result = new HashMap<>();

        if(empDtService.deleteData(id)){
            result.put("error_message","success");
        }
        else{
            result.put("error_message","failed");
        }
        return result;

    }

    @GetMapping("/queryAllData")
    public List<EmpDt> queryAllData(){
        return empDtService.queryAllData();
    }

    //分页查询所有信息
    @GetMapping("/dataInfo/{pageNum}/{pageSize}")
    public PageInfo<EmpDt> getAllAccountByPage(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize){
        //Integer pageNum = Integer.valueOf(params.get("pageNum"));
        //Integer pageSize = Integer.valueOf(params.get("pageSize"));
//        System.out.println("========");
//        System.out.println("pageNum: " + pageNum);
//        System.out.println("pageSize: " + pageSize);
//        System.out.println("========");
        return empDtService.getAllDataByPage(pageNum,pageSize);
    }

}
